﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BanhangOnline.BackEnd
{
    public partial class UsrAccount : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDataGrid();
            }
        }

        protected void BindDataGrid()
        {
            BanhangDataContext dc = new BanhangDataContext();
            if (M.GetQueryString("CompanyId",this)!=null)
            {
                var query = from a in dc.Accounts where a.CompanyId == Convert.ToInt32(M.GetQueryString("CompanyId", this)) select a;
                grdAccount.DataSource = query;
                grdAccount.DataBind();
            }
            else
            {
                var query = from a in dc.Accounts select a;
                grdAccount.DataSource = query;
                grdAccount.DataBind();
            }
        }

        protected void grdAccount_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item||e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DropDownList cboUserRole = (DropDownList) e.Item.FindControl("cboUserRole");
                M.BindEnumToCbo(cboUserRole,typeof(Enum.UserRole));
                cboUserRole.SelectedValue = DataBinder.Eval(e.Item.DataItem, "UserRole").ToString();
                DropDownList cboGender = (DropDownList)e.Item.FindControl("cboGender");
                M.BindEnumToCbo(cboGender, typeof(Enum.Gender));
                cboGender.SelectedValue = DataBinder.Eval(e.Item.DataItem, "Gender").ToString();
                DropDownList cboCompany = (DropDownList) e.Item.FindControl("cboCompany");
//                BanhangDataContext dc = new BanhangDataContext();
//                var query = from c in dc.Companies select c;
//                query.
//                cboCompany.DataSource = query;
//                cboCompany.DataTextField = "Name";
//                cboCompany.DataValueField = "Id";
//                cboCompany.DataBind();
//                cboCompany.SelectedValue = DataBinder.Eval(e.Item.DataItem, "CompanyId").ToString();
            }
        }
    }
}